<?php

namespace app\model\v1\recruit;

use app\Request;
use crmeb\traits\ModelTrait;
use think\Model;

class RecruitCompanyAccount extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
     //   'create_time','update_time','delete_time'
    ];



    /**
     * 求职招聘后台 - 企业账号管理列表
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getCompanyAccountList($param = [])
    {
        $data["data"] = self::field ("*")
            ->whereNull("delete_at")
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){
                    $wheretime = explode (',',$param['time']);
                    $query->where('created_at', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("company_name",'like', '%'.$param["word"].'%');
                if(!empty($param["pid"])) $query->where("pid",  $param["pid"]);
            })
            ->order("id", "desc")
            ->page($param['page'], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("*")
            ->whereNull("delete_at")
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){
                    $wheretime = explode (',',$param['time']);
                    $query->where('created_at', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("company_name",'like', '%'.$param["word"].'%');
                if(!empty($param["pid"])) $query->where("pid",  $param["pid"]);
            })
            ->count();

        return $data;
    }


    /**
     * 企业账号管理列表 - 添加
     * @param $param
     * @return int|string
     */
    public static function addCompanyAccount($param = [])
    {
        $company = RecruitCompany::where("id", $param["pid"])->find();
        $add = [
            "admin_id"          => $param["admin_id"],
            "company_id"        => $param["company_id"] ?? '',
            "pid"               => $param["pid"],
            "company_name"      => $company["name"] ?? '',
            "company_account"   => $param["company_account"] ?? '',
        ];
        return self::insert($add);
    }


    /**
     * 企业账号管理列表 - 编辑
     * @param $param
     * @return RecruitIndustry
     */
    public static function updateCompanyAccount($param = [])
    {
        $update = [];
        $company = RecruitCompany::where("id", $param["pid"])->find();
        $update["company_name"] = $company["name"] ?? "";

        if(!empty($param["pid"])) $update["pid"] = $param["pid"];
        if(!empty($param["company_account"])) $update["company_account"] = $param["company_account"];

        return self::where("id", $param["id"])->update($update);
    }





    public static function enableCompanyAccount($param = [])
    {
        //获取用户发送的id，根据状态修改当前banner的启用禁用状态
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }






}